package com.hengpeng.itfints.controller.help;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.itfinbase.en.ArticleGroupType;
import com.hengpeng.itfinbase.persist.Article;
import com.hengpeng.itfinbase.persist.ArticleGroup;
import com.hengpeng.itfints.controller.BaseController;
import com.hengpeng.itfints.tools.PageUtil;

@Controller
@RequestMapping(value = "/help")
public class HelpController extends BaseController {
    /**
     * 公司介绍（包括类似的页面显示）
     * 
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/profile")
    public String profile(ModelMap map, HttpServletRequest request, Long id) {
        map.put("id", id);
        return "/page/home/index.jsp";
    }

    /**
     * 帮助中心--常见问题
     * 
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/cproblem")
    public String cproblem(ModelMap map, HttpServletRequest request, Integer index) {
        if (index == null) {
            index = 1;
        }
        QueryResult<Article> articleList = serviceFactory.getArticleService().findArticleListByGroup(
                ArticleGroupType.COMMON_PROBLEM, 3, index);
        PageUtil pUtil = new PageUtil(3, (int) articleList.getTotalCount(), index);
        map.put("articleList", articleList.getResults());
        map.put("PageUtil", pUtil);
        return "/page/help/index.jsp";
    }

    /**
     * 帮助中心--理财须知
     * 
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/financial")
    public String financial(ModelMap map, HttpServletRequest request, Integer index) {
        if (index == null) {
            index = 1;
        }
        QueryResult<Article> articleList = serviceFactory.getArticleService().findArticleListByGroup(
                ArticleGroupType.FINANCIAL_INFORMATION, 3, index);
        PageUtil pUtil = new PageUtil(3, (int) articleList.getTotalCount(), index);
        map.put("articleList", articleList.getResults());
        map.put("PageUtil", pUtil);
        return "/page/help/financial.jsp";
    }

    @RequestMapping(value = "/cproblem/details")
    public String details(ModelMap map, HttpServletRequest request, Long id) {
        Article article = serviceFactory.getArticleService().getArticle(id);
        Article provarticle = serviceFactory.getArticleService().getPrevArticle(article.getPublishTime(),
                ArticleGroupType.COMMON_PROBLEM);
        Article nextarticle = serviceFactory.getArticleService().getNextArticle(article.getPublishTime(),
                ArticleGroupType.COMMON_PROBLEM);
        map.put("article", article);
        map.put("provarticle", provarticle);
        map.put("nextarticle", nextarticle);
        return "/page/help/detail.jsp";
    }

    @RequestMapping(value = "/financial/details")
    public String financialDetails(ModelMap map, HttpServletRequest request, Long id) {
        Article article = serviceFactory.getArticleService().getArticle(id);
        Article provarticle = serviceFactory.getArticleService().getPrevArticle(article.getPublishTime(),
                ArticleGroupType.FINANCIAL_INFORMATION);
        Article nextarticle = serviceFactory.getArticleService().getNextArticle(article.getPublishTime(),
                ArticleGroupType.FINANCIAL_INFORMATION);
        map.put("article", article);
        map.put("provarticle", provarticle);
        map.put("nextarticle", nextarticle);
        return "/page/help/financialDetails.jsp";
    }

    @RequestMapping(value = "/principal")
    public String principal(ModelMap map, HttpServletRequest request) {
        return "/page/home/principal/index.jsp";
    }

    @RequestMapping(value = "/problem")
    public String problem(ModelMap map, HttpServletRequest request) {
        return "/page/home/wealthinfomation/index.jsp";
    }

    @RequestMapping(value = "/interestratecal")
    public String interestratecal(ModelMap map, HttpServletRequest request) {
        return "/page/home/homeAction/index.jsp";
    }

    /**
     * 理财技巧
     * 
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "/skills")
    public String skills(ModelMap map, HttpServletRequest request, String type, Integer index) {
        if (type == null) {
            type = "FINANCIAL_SKILLS";
        }

        if (index == null) {
            index = 1;
        }
        QueryResult<Article> articleList = serviceFactory.getArticleService().findArticleListByGroup(
                ArticleGroupType.valueOf(type), 3, index);
        PageUtil pUtil = new PageUtil(3, (int) articleList.getTotalCount(), index);
        map.put("articleList", articleList.getResults());
        map.put("PageUtil", pUtil);
        map.put("type", type);
        return "/page/home/wealthinfomation/newsList.jsp";
    }

    /**
     * 理财技巧-详情页面
     * 
     * @param map
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/skills/details")
    public String skillsDetails(ModelMap map, HttpServletRequest request, Long id, String type) {
        Article article = null;
        if (id == 0) {
            article = serviceFactory.getArticleService().getArticleLike();
        } else {
            article = serviceFactory.getArticleService().getArticle(id);
        }
        Article provarticle = serviceFactory.getArticleService().getPrevArticle(article.getPublishTime(),
                ArticleGroupType.valueOf(type));
        Article nextarticle = serviceFactory.getArticleService().getNextArticle(article.getPublishTime(),
                ArticleGroupType.valueOf(type));
        map.put("article", article);
        map.put("provarticle", provarticle);
        map.put("nextarticle", nextarticle);
        ArticleGroup group = serviceFactory.getArticleService().getArticleByGroupType(ArticleGroupType.valueOf(type));
        map.put("group", group);
        return "/page/home/wealthinfomation/index.jsp";
    }
}
